import { getTreeNodeParents } from '../utils'
import { cloneDeep } from 'lodash-es'
const useFolderClick = (initInfo, treeRef, getSourceList, resetSelectedInfo, emit) => {
  // 点击树结构文件夹事件
  const treeNodeClick = v => {
    if (initInfo.value.isRecycle) {
      initInfo.value.isCard = initInfo.value.isOldCard
      initInfo.value.isRecycle = false
    }
    treeRef.value.setCheckedKeys([v.id], false)
    initInfo.value.form.folder_id = v.id
    initInfo.value.breadcrumbList = getTreeNodeParents(cloneDeep(initInfo.value.folderList), v)
    initInfo.value.breadcrumbList.push(v)
    initInfo.value.defaultExpandNodes = []
    initInfo.value.breadcrumbList.forEach(item => {
      initInfo.value.defaultExpandNodes.push(item.id)
    })
    resetSelectedInfo()
    getSourceList()
  }

  // 右键点击树结构文件夹事件
  const treeNodeRightClick = v => {
    if (!initInfo.value.isChooseResource) {
      initInfo.value.rightClickFolder = v.data
    }
  }

  // 表格类型布局单元格双击事件 （针对文件夹的双击，进入该文件夹）
  const cellDblclickHandler = (row, column) => {
    if (initInfo.value.isChooseResource) {
      if (row.isFolder) {
        treeNodeClick(row)
      } else {
        emit('select', [row])
      }
    } else {
      if (row.isFolder) {
        if (column.property !== 'title' && column.property !== 'operate') {
          treeNodeClick(row)
        }
      }
    }
  }

  // 卡片类型布局的双击事件（针对文件夹的双击，进入该文件夹）
  const dblClickRow = v => {
    if (v.isFolder) {
      treeNodeClick(v)
    } else {
      if (initInfo.value.isChooseResource) {
        emit('select', [v])
      }
    }
  }

  return {
    treeNodeClick,
    treeNodeRightClick,
    cellDblclickHandler,
    dblClickRow
  }
}
export default useFolderClick
